<?php

namespace app\common\model;

/**
 * 用户收货地址模型
 * Class UserAddress
 * @package app\common\model
 */
class UserAddress extends BaseModel
{
    protected $name = 'user_address';

    /**
     * 追加字段
     * @var array
     */
    protected $append = ['region'];

    /**
     * 地区名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getRegionAttr($value, $data)
    {
        if (isset($data['province_id']) &&
            isset($data['city_id']) &&
            isset($data['district']) &&
            isset($data['region_id'])) {
            return [
                'province' => Region::getNameById($data['province_id']),
                'city' => Region::getNameById($data['city_id']),
                'region' => $data['region_id'] == 0 ? $data['district']
                    : Region::getNameById($data['region_id']),
            ];
        }
    }

}
